import { type RouteLocationNormalized } from 'vue-router'
import { defineStore } from 'pinia'
import { store } from '@/store'
import { TABS_ROUTES } from '@/enums/cacheEnum'
import router from '@/router'
import { useKeepAliveStore } from './keepAlive'
import { LOGIN_NAME, REDIRECT_NAME, PAGE_NOT_FOUND_NAME } from '@/router/constant'

interface TabsViewState {
  // 标签页
  tabsList: RouteLocationNormalized[]
}

// 不需要出现在标签页中的路由
export const blankList = [LOGIN_NAME, REDIRECT_NAME, PAGE_NOT_FOUND_NAME] as const

export const useTabsViewStore = defineStore({
  id: 'tabs-view',
  state: (): TabsViewState => ({
    tabsList: []
  }),
  getters: {
    getTabsList: (state) => {
      return state.tabsList.filter(item => {
        // js 运算符 感叹号后置 是断言
        return !item.meta?.hideInTabs && router.hasRoute(item.name!)
      })
    },
  },
  actions: {
    delCompFormClosedTabs(closedTabs: RouteLocationNormalized[]){

    },
    // 初始化标签页
    initTabs(routes: RouteLocationNormalized[]){
      this.tabsList = routes
    },
    // 添加标签页
    addTabs(route: RouteLocationNormalized){
      if (blankList.includes(route.name)) return false
      const isExists = this.tabsList.some(item => item.fullPath == route.fullPath)
      if (!isExists) {
        this.tabsList.push(route)
      }
      return true
    },
    // 关闭左侧
    closeLeftTbas(){},
    // 关闭右侧
    closeRightTbas(){},
    // 关闭其他
    closeOtherTabs(){},
    // 关闭当前
    closeCurrentTabs(){},
    // 关闭全部
    closeAllTabs(){},
    // 更新tab标题
    updateTabTitle(){},
  }
})




